<?php
class User extends Controller {

	function User()
	{
		parent::Controller();
	}

	function index()
	{
		redirect('');
	}
	function login()
	{
		if($this->users->isLoggedIn())
		{
			echo '<script type="text/javascript">
				window.location = "' . base_url() . 'index.php"
			</script>';
		}
		else
		{
			if($this->input->post('username')!='' && $this->input->post('password')!='')
			{
				if( !$this->users->login($this->input->post('username'),$this->input->post('password')) )
				{
					$error = 1;
					$data['err'] = TRUE;
					$data['effect'] = '$("form").effect("shake", { times: 2, direction: "left", distance: "8" }, 60);';
				}
			}
	
			//Check if the user is already logged in
	
			$data['title'] = $this->db->query('SELECT * FROM project LIMIT 1');
			echo 'hello';
			$this->load->view('loginform', $data);
		}
	}

	function register()
	{
		$this->load->view('register');
		$this->users->checkAdmin();
	}
	function do_register() 
	{
	$this->users->checkAdmin();
	if($this->input->post('userregister'))
		{
			if($this->input->post('username')!='')
			{
				$error = "Please enter a username.";
			}

			if($this->input->post('email')!='')
			{
				$error = "Please enter your email.";
			}

			if($this->input->post('password')!=$this->input->post('password2'))
			{
				$error = "Passwords do not match.";
			}
			$username = trim($this->input->post('username'));
			$email = trim($this->input->post('email'));
			$password = trim($this->input->post('password'));
			$admin = $_POST['admin'];
			$fname = $_POST['fname'];
			if(!$this->users->register($username,$password,$email,$fname,$admin))
			{
				$error = $this->users->last_error;
			}
			else
			{
				redirect('');
			}
		}
		else {
		redirect('register');
		}
	}

	function registered()
	{
	$this->users->checkAdmin();
		$data = array(
			'header_img'=>'header_logo.gif',
			'show_nav'=>false
			);
		$output = $this->load->view('common/header', $data, true);
		$output .= $this->load->view('user/registered', $data, true);
		$output .= $this->load->view('common/footer', $data, true);

		$this->output->set_output($output);
	}

	function logout()
	{
		ob_start();
		$this->users->logout();
		//echo '<script type="text/javascript">window.location = "' . base_url() . 'index.php"</script>';
		ob_flush();
	}
}
?> 